package com.linkoog.devtools.intellij.ui.table.column_info.column;

import com.linkoog.devtools.intellij.ui.table.cell_editor.JCheckBoxTableCellEditor;
import com.linkoog.devtools.intellij.ui.table.cell_reander.JCheckBoxTableCellRenderer;
import com.linkoog.devtools.intellij.ui.table.TableRowData;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.TableCellValueGetterProvider;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.TableCellWidthProvider;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.getter.FieldTableCellValueGetterProvider;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.setter.FieldTableCellValueSetterProvider;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class CheckBoxColumnInfo<T extends TableRowData> extends AbstractColumnInfo<T, Boolean> {

    private final TableCellWidthProvider tableCellWidthProvider;
    private TableCellValueGetterProvider<T> tableCellValueGetterProvider;
    private FieldTableCellValueSetterProvider<T> tableCellValueSetterProvider;

    public CheckBoxColumnInfo(String columnName, String fieldName, TableCellWidthProvider tableCellWidthProvider) {
        super(columnName);
        this.tableCellWidthProvider = tableCellWidthProvider;
        tableCellValueGetterProvider = new FieldTableCellValueGetterProvider<>(fieldName);
        tableCellValueSetterProvider = new FieldTableCellValueSetterProvider<>(fieldName);
    }

    @Override
    public int getWidth(JTable table) {
        if (tableCellWidthProvider == null) return -1;
        return tableCellWidthProvider.getWidth(table);
    }

    @Nullable
    @Override
    public Boolean valueOf(T rowData) {
        return Boolean.TRUE.equals(tableCellValueGetterProvider.valueOf(rowData));
    }

    @Override
    public void setValue(T rowData, Boolean value) {
        tableCellValueSetterProvider.setValue(rowData, value);
    }

    @Override
    public Class<Boolean> getColumnClass() {
        return Boolean.class;
    }

    @Override
    public boolean isCellEditable(final T rowData) {
        return true;
    }


    @Override
    public @Nullable TableCellEditor getEditor(T rowData) {
        return new JCheckBoxTableCellEditor();
    }

    @Override
    public @Nullable TableCellRenderer getRenderer(T rowData) {
        return new JCheckBoxTableCellRenderer();
    }
}
